//
//  MainViewController.swift
//  weibo001
//
//  Created by ios on 15/11/3.
//  Copyright © 2015年 ios. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
 //添加点击方法
    @objc private func clickComposeBtn() {
    
    print("come on!!")
    }
    
    // MARK:视图生命周期函数
    override func viewDidLoad() {
        super.viewDidLoad()

    addChildViewControllers()
     addComposeButton()
        
        
    }

    override func viewWillAppear(animated: Bool) {
     //父类要初始化
        
        super.viewWillAppear(animated)
        
        tabBar.bringSubviewToFront(composeButton)
    }
    
    
    // MARK:懒加载创建按钮
 
    private lazy var composeButton:UIButton = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
    
    
 //   private lazy var composeButton:UIButton = {
    
//    let button = UIButton()
//    
//        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal )
//         button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted )
//        
//        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
//         button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
//        
//    
//    return button
//    }()
        
 
}

//MARK: 设置界面
extension MainViewController {
    
    //让按钮显示出来
    
    private func addComposeButton() {
    
    // 添加按钮
    tabBar.addSubview(composeButton)
        
    let count = childViewControllers.count
        
        let w = tabBar.bounds.width / CGFloat(count)
    
    composeButton.frame = CGRectInset(tabBar.bounds, 2*w, 0)
        
        // 监听点击按钮
    
        composeButton.addTarget(self, action: "clickComposeBtn", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    
    
    
    //添加所有的控制器
    private func addChildViewControllers() {
        
        //设置渲染颜色
      //  tabBar.tintColor = UIColor.orangeColor()
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName:"tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName:"tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName:"tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName:"tabbar_profile")
        
    }
    
    
    
    // 添加子控制器
   private func addChildViewController(vc: UIViewController, title:String, imageName:String)
    {
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }
    
    
}
